import collections
import sortedcontainers
class MovieRentingSystem(object):

    def __init__(self, n, entries):
        """
        :type n: int
        :type entries: List[List[int]]
        """
        self. store = set()
        self.movies = collections.defaultdict(sortedcontainers.SortedList)
        self.price = collections.defaultdict(int)
        for s, m, p in entries:
            self.store.add((s, m))
            self.movies[m].add([p, s])
            self.price[(s, m)] = p
        self.out = sortedcontainers.SortedList()

    def search(self, movie):
        """
        :type movie: int
        :rtype: List[int]
        """
        ans = []
        index = 0
        while index < 5 and index < len(self.movies[movie]):
            if ((self.movies[movie][index][1], movie) not in self.store) or (index > 0 and self.movies[movie][index] == self.movies[movie][index - 1]):
                self.movies[movie].pop(index)
            else:
                ans.append(self.movies[movie][index][1])
                index += 1
        return ans


    def rent(self, shop, movie):
        """
        :type shop: int
        :type movie: int
        :rtype: None
        """
        self.store.remove((shop, movie))
        self.out.add([self.price[(shop, movie)], shop, movie])

    def drop(self, shop, movie):
        """
        :type shop: int
        :type movie: int
        :rtype: None
        """
        self.store.add((shop, movie))
        self.movies[movie].add([self.price[(shop, movie)], shop])


    def report(self):
        """
        :rtype: List[List[int]]
        """
        index = 0
        ans = []
        while index < 5 and index < len(self.out):
            if (self.out[index][1], self.out[index][2]) in self.store or (index > 0 and self.out[index] == self.out[index - 1]):
                self.out.pop(index)
            else:
                ans.append([self.out[index][1], self.out[index][2]])
                index += 1
        return ans

